﻿/////////////////////////////////////////////////////////////////
//File: ArticleInfo.cs
//Description: 
//  Ajax 代码
//  
//Copyright© 2011 Moliu
//History:
//  2011-2-23 23:28:31    CodeSmith5.0.1    Original Version
/////////////////////////////////////////////////////////////////

using System;
using System.Threading;
using System.Collections.Generic;
using System.IO;
using MAF.Common;
using My.Blog.Domain.Entities;
using My.Blog.Domain.Interfaces;
using My.Blog.Business.Service;
using My.Blog.Business.Security;
using My.Blog.Web.Controllers;
using System.Configuration;
using System.Collections.Specialized;
using System.Web;

namespace My.Blog.Web.UI_MAF_Template
{
    public class ArticleDetail : My.Blog.Web.Controllers.BasePage
    {
        public ArticleDetail()
        {
            HttpContext context = HttpContext.Current;

            // 标签
            const string IF_EMPTY = "If:Empty";
            const string IF_NOT_EMPTY = "IfNot:Empty";
            const string IF_MAINTAIN = "If:Maintain";

            // 模板路径及逻辑标签正则表达式
            string TPL_FILE_PATH = ConfigurationManager.AppSettings["TemplatesPath"] + @"/ArticleDetail.html";
            const string REG_LOGIC = @"(?is)<(Tag>)(.*?)</\1";

            // 主模板
            string tpl = File.ReadAllText(context.Server.MapPath(TPL_FILE_PATH));
            Template tplLogic = new Template(tpl, REG_LOGIC);
            Template tplEntity;

            // 数据源
            ArticleInfo info = new ArticleHandler(this).ReadArticleByConditions();

            // 模板替换
            if (null == info)
            {
                tplLogic.Replace(IF_NOT_EMPTY, "");
                context.Response.Write(tplLogic.Result);
            }
            else
            {
                tplLogic.Replace(IF_EMPTY, "");
                if (true != this.IsMasterMode)
                    tplLogic.Replace(IF_MAINTAIN, "");

                tplEntity = new Template(tplLogic.Result);
                NameValueCollection infoMap = MAF.Common.ObjectConverter.ToNameValueCollection<ArticleInfo>(info, @"{Key}", "");

                infoMap["{RootUrl}"] = this.RootUrl;
                tplEntity.Replace(infoMap);

                context.Response.Write(tplEntity.Result);
            }
        }
    }
}

